/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-07 11:01:34
 * @LastEditTime: 2022-07-09 18:09:27
 * @LastEditors: liutq
 * @Reference:
 */
package system

import (
	"time"

	"github.com/maxyang107/jinglan/model"
)

type SysConfig struct {
	ID          uint      `gorm:"column:id; parimary_key"`
	ConfigKey   string    `gorm:"column:config_key;"`
	ConfigValue string    `gorm:"column:config_value;" `
	ConfigDesc  string    `gorm:"column:config_desc;" `
	Createtime  time.Time `gorm:"column:createtime;"`
}

type ConfigList struct {
	Data []SysConfig
}

func AddOrUpdateConfig(conf SysConfig) error {
	if conf.ID > 0 {
		if err := model.Db.Model(&SysConfig{}).Update(&conf).Error; err != nil {
			return err
		}
	} else {
		conf.Createtime = time.Now()
		if err := model.Db.Model(&SysConfig{}).Create(&conf).Error; err != nil {
			return err
		}
	}
	return nil
}

func GetSysConfigList() *ConfigList {
	var list ConfigList
	model.Db.Model(&SysConfig{}).Scan(&list.Data)

	return &list
}

func GetSysConfigDetail(configKey string) SysConfig {
	var detail SysConfig
	model.Db.Where(&SysConfig{ConfigKey: configKey}).First(&detail)

	return detail
}
